package us.webone.webone.controller;


import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import us.webone.webone.entity.Recommendation;
import us.webone.webone.service.IRecommendationService;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author none
 * @since 2021-06-30
 */
@RestController
@RequestMapping("/api/recommendation")
public class RecommendationController {

    IRecommendationService recommendationService;

    /**
     * 添加推荐
     */
    @PostMapping("/add")
    public ResponseEntity<String> addRecommendation(@RequestBody Map<String, String> map) {
        long id = Long.parseLong(map.get("id"));
        String website = map.get("website");
        String url = map.get("url");

        Recommendation newRecommendation = new Recommendation();
        newRecommendation.setId(id);
        newRecommendation.setWebsite(website);
        newRecommendation.setUrl(url);

        recommendationService.saveOrUpdate(newRecommendation);
        return new ResponseEntity<>("添加成功", HttpStatus.OK);
    }

    /**
     * 删除推荐
     */
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteRecommendation(@PathVariable("id") Long id) {
        recommendationService.removeById(id);
        return new ResponseEntity<>("删除成功", HttpStatus.OK);
    }

    /**
     * 获取推荐
     */
    @GetMapping("/get")
    public ResponseEntity<List<Recommendation>> getRecommendation() {
        List<Recommendation> list = recommendationService.list();
        return new ResponseEntity<>(list, HttpStatus.OK);
    }
}
